﻿using System;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GradeApp.Views;
using GradeApp.Web;
using GradeApp.Web.Services;
using System.Linq;

namespace GradeApp.ViewModels
{
    public class CourseConfigurationViewModel : ViewModelBase
    {
        private GradeAppDomainContext _gradeAppDomainContext;

        private CourseConfigurations _courseConfiguration;
        public CourseConfigurations CourseConfiguration
        {
            get { return _courseConfiguration; }
            set
            {
                if (_courseConfiguration != value)
                {
                    _courseConfiguration = value;
                    OnPropertyChanged("CourseConfiguration");
                }
            }
        }


        public CourseConfigurationViewModel()
        {
            _gradeAppDomainContext = new GradeAppDomainContext();
            _gradeAppDomainContext.Load(_gradeAppDomainContext.GetCourseConfigurationsQuery(), UpdateSettings,
                                        null);
        }

        private void UpdateSettings(LoadOperation<CourseConfigurations> op)
        {
            CourseConfiguration =
                op.Entities.SingleOrDefault(course => course.Id == GradeAppContext.CurrentCourse.CourseConfiguration_Id);
        }

        public void SaveSettings()
        {
            _gradeAppDomainContext.SubmitChanges();
        }
    }
}
